package com.zhugang.week01;

/**
 * @program algorithms
 * @description: reverseString
 * @author: chanzhugang
 * @create: 2022/05/21 22:13
 */
public class ReverseString {

    /**
     * 344.反转字符串
     *
     * @param s
     */
    public void reverseString(char[] s) {
        // 遍历一半，交换位置
        int n = s.length;
        for (int i = 0; i < n / 2; i++) {
            char temp = s[i];
            s[i] = s[n - 1 - i];
            s[n - 1 - i] = temp;
        }
    }

    public void reverseString2(char[] s) {
        // 双指针，i，j 交换，对应i++ ，j--
        int i = 0;
        int j = s.length - 1;
        while (i <= j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }

}